package com.oasis.cloud.app.config;

import com.oasis.cloud.core.sms.filter.SmsAbstractAuthenticationProcessingFilter;
import com.oasis.cloud.core.sms.manager.SmsAuthenticationManager;
import com.oasis.cloud.core.sms.provider.SmsAuthenticationProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableResourceServer
public class OasisAuthResourceConfig extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        //super.configure(http);
        http.authorizeRequests().antMatchers("/getUserMessage","/getCode").permitAll();
        http.authenticationProvider(smsAuthenticationProvider()).addFilterBefore(getSmsAbstractAuthenticationProcessingFilter(), UsernamePasswordAuthenticationFilter.class);

    }

    private SmsAbstractAuthenticationProcessingFilter getSmsAbstractAuthenticationProcessingFilter() {

        SmsAuthenticationManager smsAuthenticationManager = new SmsAuthenticationManager();
        SmsAbstractAuthenticationProcessingFilter smsAbstractAuthenticationProcessingFilter = new SmsAbstractAuthenticationProcessingFilter("/loginBySmsCode");
        smsAbstractAuthenticationProcessingFilter.setAuthenticationManager(smsAuthenticationManager);
        return smsAbstractAuthenticationProcessingFilter;
    }


    private SmsAuthenticationProvider smsAuthenticationProvider() {
        SmsAuthenticationProvider smsAuthenticationProvider = new SmsAuthenticationProvider();
        return smsAuthenticationProvider;
    }

}
